package com.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 205. 同构字符串
 * 使用两个HashMap来记录对应关系
 *
 * @author fy
 * @date 2022/4/6 16:08
 */
public class Solution205 {

    public boolean isIsomorphic(String s, String t) {
        if (s == null || t == null) {
            return false;
        }
        if (s.length() != t.length()) {
            return false;
        }

        Map<Character, Character> sMap = new HashMap<>(s.length() * 2);
        Map<Character, Character> tMap = new HashMap<>(s.length() * 2);
        for (int i = 0; i < s.length(); i++) {
            char sChar = s.charAt(i);
            char tChar = t.charAt(i);
            if (sMap.containsKey(sChar)) {
                if (sMap.get(sChar) != tChar) {
                    return false;
                }
            } else {
                sMap.put(sChar, tChar);
            }

            if (tMap.containsKey(tChar)) {
                if (tMap.get(tChar) != sChar) {
                    return false;
                }
            } else {
                tMap.put(tChar, sChar);
            }
        }
        return true;
    }

    public static void main(String[] args) {
//        String s = "paper";
//        String t = "title";
        String s = "badc";
        String t = "baba";
        boolean res = new Solution205().isIsomorphic(s, t);
        System.out.println(res);
    }

}
